//
//  NSDate+ROCaculateTime.m
//  Runone
//
//  Created by runone02 on 16/12/14.
//  Copyright © 2016年 runone. All rights reserved.
//

#define D_MINUTE	60
#define D_HOUR		3600
#define D_DAY		86400
#define D_WEEK		604800
#define D_YEAR		31556926

#import "NSDate+ROCaculateTime.h"
#import <UIKit/UIKit.h>

@implementation NSDate (ROCaculateTime)

- (NSUInteger) daysOfMonth {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];//
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:self];
    return range.length;
}

- (NSUInteger) daysOfYear {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *coms = [calendar components:NSCalendarUnitYear fromDate:self];
    
    NSUInteger days ;
    for (int i = 0; i < 12; i++) {
        [coms setMonth:i];
        days += [[calendar dateFromComponents:coms] daysOfMonth];//获取每个月份组件的天数
    }
    return days;
}

- (NSDate *) firstDayOfMonth {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    unsigned unitsFlag = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | kCFCalendarUnitSecond;
    NSDateComponents *coms = [calendar components:unitsFlag fromDate:self];
    [coms setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; //设置时区
    [coms setDay:1]; //获取第一天
    return [calendar dateFromComponents:coms];
}

- (NSDate *) lastDayOfMonth {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    unsigned unitsFlag = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | kCFCalendarUnitSecond;
    NSDateComponents *coms = [calendar components:unitsFlag fromDate:self];
    [coms setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; //设置时区
    [coms setDay:[self daysOfMonth]]; //获取最后一天
    return [calendar dateFromComponents:coms];
}

- (NSDate *) caculateMonthsAndDays:(NSUInteger )months  days:(NSUInteger )days {
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *coms = [[NSDateComponents alloc] init];
    [coms setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [coms setMonth:months];
    [coms setMonth:days];
    return [calendar dateByAddingComponents:coms toDate:self options:NSCalendarWrapComponents];
}

- (NSArray *) caculateMonthsAndDaysBetweenTwoDates:(NSDate *)toDate {
    NSMutableArray *monthsAndDays = [[NSMutableArray alloc] init];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    unsigned unitsFlag = NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *comps = [calendar components:unitsFlag fromDate:self toDate:toDate options:0];
    
    [monthsAndDays addObject:[NSString stringWithFormat:@"%ld", (long)[comps month]]];
    [monthsAndDays addObject:[NSString stringWithFormat:@"%ld", (long)[comps day]]];
    return monthsAndDays;
}

- (NSInteger ) weekday {
    return [[[NSCalendar currentCalendar] components:NSCalendarUnitWeekday fromDate:self] weekday];
}

- (NSString *) weekdayName:(BOOL)isShortName localIdentifier:(NSString *)identifier {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone   = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
    formatter.locale     = [[NSLocale alloc] initWithLocaleIdentifier:identifier];
    formatter.dateFormat = isShortName ? @"EE" : @"EEEE";
    return [formatter stringFromDate:self];
}

//- (NSString *) weekdayNameEN:(BOOL)isShortName {
//
//}

- (BOOL) TwoDateIsIn24HourTODate:(NSDate *)toDate {
    NSTimeInterval ti = [self timeIntervalSinceDate:toDate];
    CGFloat inter   = (CGFloat) (fabs(ti) / D_DAY);
    if (inter > 1) {
        return NO;
    }else {
    return YES;
    }
}

- (NSString *)theDaysTODate:(NSDate *)toDate {
    NSTimeInterval ti = [self timeIntervalSinceDate:toDate];
    NSInteger dayInter   = (NSInteger) (fabs(ti) / D_DAY);
    NSInteger hourInter  = (NSInteger) ((NSInteger)fabs(ti) % D_DAY) / D_HOUR;
    NSString *str  = [NSString stringWithFormat:@"%ld天%ld小时",(long)dayInter, (long)hourInter];
    return str;
}

- (NSInteger)compareToCurrentDate {
    NSInteger ter = 0 ;
    NSDate *currentDate = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone]; // 获得系统的时区
    NSTimeInterval time = [zone secondsFromGMTForDate:currentDate];// 以秒为单位返回当前时间与系统格林尼治时间的差
    NSDate *nowDate = [currentDate dateByAddingTimeInterval:time];
    NSTimeInterval ti = [self timeIntervalSinceDate:nowDate];
    if (ti < 0) {
        ter = -1;//小于当前时间
    }
    if (ti == 0) {
        ter = 0; //等于当前时间
    }
    if (ti > 0) {
       ter = 1; //大于当前时间
    }
    return ter;
}

- (NSInteger)compareToToDate:(NSDate *)toDate {
    NSInteger ter = 0 ;
    NSTimeInterval ti = [self timeIntervalSinceDate:toDate];
    if (ti < 0) {
        ter = -1;//小于
    }
    if (ti == 0) {
        ter = 0; //等于
    }
    if (ti > 0) {
        ter = 1; //大于
    }
    return ter;
}



@end
